package com.tamartech.planning.importer;

import java.util.Date;

import org.apache.log4j.Logger;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;

import uk.me.jstott.jcoord.LatLng;
import uk.me.jstott.jcoord.OSRef;

import com.tamartech.planning.dao.ApplicationDAO;
import com.tamartech.planning.openly.Application;
import com.tamartech.planning.openly.ApplicationFactory;
import com.tamartech.planning.openly.ApplicationList;
import com.tamartech.planning.openly.ApplicationSummary;

public class OpenlyLocalImporter
{
    private static Logger log = Logger.getLogger (OpenlyLocalImporter.class);

    private static final String PLANNING_APP_LIST = "http://openlylocal.com/councils/357/planning_applications.json";

    private boolean include (Application app)
    {
        String ward = app.getPlanning_application().getOther_attributes().getWard_name();
        
        return (ward.indexOf("Redhill") >= 0);
    }
    
    public void run() throws Exception
    {
        ClientRequest req = new ClientRequest(PLANNING_APP_LIST);

        ClientResponse<ApplicationList> res = req.get(ApplicationList.class);
        ApplicationList result = res.getEntity();
        System.out.println("Count " + result.getPlanning_applications().length);
        
        for (ApplicationSummary appSum : result.getPlanning_applications())
        {
            Application app = ApplicationFactory.get(appSum.getId());
            
            if (include (app))
            {
                Long openlyLocalId = null;
                if (app.getPlanning_application().getId() != null)
                    openlyLocalId = app.getPlanning_application().getId().longValue();
                
                boolean found = true;
                
                ApplicationDAO dao = new ApplicationDAO();

                com.tamartech.planning.data.Application dbApp = dao.getByOpenlyLocalId(openlyLocalId);
                        
                if (dbApp == null)
                {
                    found = false;
                    dbApp = new com.tamartech.planning.data.Application();
                }
                        
                OSRef osRef = new OSRef (-1.0,-1.0);
                LatLng latLng = new LatLng (-1.0, -1.0);
                
                if (app.getPlanning_application().getLat() != null && app.getPlanning_application().getLng() != null)
                {
                    latLng = new LatLng (app.getPlanning_application().getLat(), app.getPlanning_application().getLng());
                    osRef = latLng.toOSRef();
                }
                
                if (app.getPlanning_application().getAddress() != null)                
                    dbApp.setAddress(app.getPlanning_application().getAddress());
                
                if (app.getPlanning_application().getDescription() != null)
                    dbApp.setDescription(app.getPlanning_application().getDescription());
                
                dbApp.setOpenlyLocalId (openlyLocalId);
                
                if (app.getPlanning_application().getUid() != null)
                    dbApp.setCouncilId(app.getPlanning_application().getUid());
                
                if (latLng.getLat() >= 0.0)
                {
                    dbApp.setLatitude(latLng.getLat());
                    dbApp.setLongitude(latLng.getLng());
                }
                
                if (osRef.getEasting() >= 0.0)
                {
                    dbApp.setEasting(osRef.getEasting());
                    dbApp.setNorthing(osRef.getNorthing());
                }
                
                if (app.getPlanning_application().getPostcode() != null)
                    dbApp.setPostcode(app.getPlanning_application().getPostcode());
                
                if (app.getPlanning_application().getUrl() != null)
                    dbApp.setCouncilURL(app.getPlanning_application().getUrl());
                
                if (app.getPlanning_application().getOpenlylocalUrl() != null)
                    dbApp.setOpenlyLocalURL(app.getPlanning_application().getOpenlylocalUrl());
                
                if (app.getPlanning_application().getOther_attributes().getWard_name() != null)
                    dbApp.setWard(app.getPlanning_application().getOther_attributes().getWard_name());
                
                if (!found)
                    dbApp.setRetrieved(new Date());
                
                log.info ("Ward: " + dbApp.getWard() + ". Address: " + dbApp.getAddress() + ". New: " + !found);
                
                if (found)
                    dao.update(dbApp);
                else
                    dao.save(dbApp);
/*                
                System.out.println ("Uid: " + app.getPlanning_application().getUid() + 
                                    ". Id: " + app.getPlanning_application().getId() +
                                    ". Ward: " + app.getPlanning_application().getOther_attributes().getWard_name() +
                                    ". (Lat: " + latLng.getLat() + ". Lng: " + latLng.getLng() +")" +
                                    ". (E: " + osRef.getEasting() + ". N: " + osRef.getNorthing() + ")" +
                                    ". Address: " + app.getPlanning_application().getAddress() +
                                    ". Desc: " + app.getPlanning_application().getDescription() +
                                    ". URL: " + app.getPlanning_application().getUrl());
*/
                
            }
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub

    }

}
